/**
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-14 17:39:31
 * @Description  : 考试接口控制器
 */
const Mysequelize = require('@/models')
const R = require('@/models/Response')
const { Exam } = require('@/models/model')
const { ERROR_USER_CODE } = require('@/constants/errorCode')
const { generatId } = require('@/utils/idUtil')
const { delModel, sortModel } = require('@/utils/controllerCommon')

/**
 * 列表查询考试接口
 * @param {*} ctx
 * @returns
 */
module.exports.listExam = async (ctx) => {
  const orderCondition = [['sort']]
  const { page, pageSize, pid } = ctx.query
  const start = (page - 1) * pageSize
  const whereCondition = {}
  if (pid !== undefined) {
    whereCondition.pid = pid
  }
  const { rows, count } = await Exam.findAndCountAll({
    where: whereCondition,
    offset: +start,
    include: [
      {
        model: Exam,
        as: 'ParentExam'
      }
    ],
    limit: +pageSize,
    order: orderCondition,
    distinct: true
  })

  // 对rows的pid进行处理

  ctx.body = R.success({
    page,
    pageSize,
    total: count,
    pages: Math.ceil(count / pageSize),
    items: rows
  })
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 创建考试接口
 * @param {*} ctx
 */
module.exports.createExam = async (ctx) => {
  const { name, pid, pic, price } = ctx.request.body
  const t = await Mysequelize.transaction()
  const nextId = generatId()
  const sort = await Exam.max('sort')

  let createObj = {
    id: nextId,
    name,
    pid,
    price: price || 100,
    pic: price || '',
    sort: sort + 1,
    createUser: ctx.user.id,
    updateUser: ctx.user.id
  }
  try {
    // 到这里可以进行考试的添加了,排序字段默认是最大值
    const exam = await Exam.create(createObj, {
      transaction: t
    })
    // 操作考试对应的权限
    await t.commit()
    ctx.body = R.success()
  } catch (error) {
    await t.rollback()
    ctx.body = R.error(ERROR_USER_CODE.EXAM_CREATE_ERROR)
  }
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 更新考试接口
 * @param {*} ctx
 */
module.exports.updateExam = async (ctx) => {
  const t = await Mysequelize.transaction()
  const { examId, name, pid, pic, price } = ctx.request.body
  const updateObj = {
    name
  }
  if (pid) {
    updateObj.pid = pid
  }
  if (pic) {
    updateObj.pic = pic
  }
  if (price) {
    updateObj.price = price
  }

  try {
    // 进行考试信息的更新
    await Exam.update(updateObj, {
      where: {
        id: BigInt(examId)
      },
      transaction: t
    })

    await t.commit()
    ctx.body = R.success()
  } catch (e) {
    await t.rollback()
    ctx.body = R.error(ERROR_USER_CODE.EXAM_UPDATE_ERROR)
  }
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 批量删除考试接口
 * @param {*} ctx
 */
module.exports.delExams = async (ctx) => {
  await delModel(Exam, ERROR_USER_CODE.EXAM_DEL_ERROR, 'examIds')(ctx)
}

/**
 * @author: Hejh(3010733382@qq.com)
 * @description: 考试排序接口
 * @param {*} ctx
 */
module.exports.sortExams = async (ctx) => {
  // 传递的考试ID顺序即为排序顺序，取出这些ID对应的sort，sort从小到大排序后，依次赋值
  await sortModel(Exam, ERROR_USER_CODE.EXAM_SORT_ERROR, 'examIds')(ctx)
}

module.exports.getExamById = async (ctx) => {
  const { examId } = ctx.query
  const exam = await Exam.findByPk(BigInt(examId))
  if (!exam) {
    ctx.body=R.error(ERROR_USER_CODE.EXAM_ID_ERROR)
    return 
  }
  ctx.body=R.success({ exam })
  
}
